from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from database import engine, Base
from route.user_api import router as user_router
from route.base_api import router as base_router
from route.cdn_config_api import router as cdn_router
from route.cdn_node_api import router as cdn_node_router
from route.cname_api import router as cname_router
from route.certificate import router as cert_route
from route.command_api import router as command_route
import logging

logging.basicConfig(
    level=logging.INFO,  # 默认级别
    format="%(asctime)s - %(name)s - %(levelname)s - %(filename)s:%(lineno)d - %(message)s",
)

logger = logging.getLogger("fast-run")  # 自定义应用 logger

# Create database tables
Base.metadata.create_all(bind=engine)

app = FastAPI()

# Configure CORS
# app.add_middleware(
#     CORSMiddleware,
#     allow_origins=["*"],  # In production, replace with your frontend URL
#     allow_credentials=True,
#     allow_methods=["*"],
#     allow_headers=["*"],
# )

# Include routers
app.include_router(base_router)
app.include_router(user_router, prefix="/api")
app.include_router(cdn_router, prefix="/api/config/cdn")
app.include_router(cdn_node_router, prefix="/api/config/cdn/node")
app.include_router(cname_router, prefix="/api/config/cdn/arecord")
app.include_router(cert_route, prefix="/api/certificates")
app.include_router(command_route, prefix="/api/commands")

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("main:app", host="127.0.0.1", port=5000, reload=True)